package com._21cn.cms.site.web.controller;

import com._21cn.cms.site.entity.RegionObject;
import com._21cn.cms.site.service.RegionObjectService;

import com._21cn.framework.http.HttpRequestInfo;
import com._21cn.framework.flip.PageList;
import com._21cn.framework.base.controller.BaseController;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin/site/regionObject")
public class RegionObjectController extends BaseController{

	private static String listView = "/admin/site/regionObject/list";
    private static String showView = "/admin/site/regionObject/show";
    private static String jsonView = "/admin/site/regionObject/json";
    private static String formView = "/admin/site/regionObject/form";
    private static String redirectListView = "redirect:/admin/site/regionObject/list.do";

    private Map<String, String> propertyTitleMap = null;

    @Autowired
    private RegionObjectService regionObjectService;
	
	@Autowired(required = false)
    @Qualifier("regionObjectValidator")
    private Validator regionObjectValidator;

    @RequestMapping("/list")
    public String listRegionObject(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter( "view", listView);
        setRequestModelMap( request, model, true );
        return view;
    }

    @RequestMapping("/search")
    public String searchRegionObject(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter( "view", jsonView );
        setRequestModelMap( request, model, true );
        PageList<RegionObject> result = getPageList( model );
        model.addAttribute( "objListJson" , com.alibaba.fastjson.JSON.toJSONString(result, SerializerFeature.WriteDateUseDateFormat) );
        return view;
    }

    @RequestMapping("/show")
    public String showRegionObject(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter( "view", showView );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id > 0 ){
            RegionObject regionObject = regionObjectService.getById( id );
            if( regionObject == null ){
                model.put( "error.param.wrong", "无效参数" );
            }
//            model.addAttribute( "statusMap", FormSelector.getStatusMap());
            model.addAttribute( "regionObject", regionObject );
        }
        setRequestModelMap(request, model);
        return view;
    }
	
	@RequestMapping("/edit")
    public String editRegionObject(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        String view = reqInfo.getParameter( "view", formView );
        Integer id = reqInfo.getIntParameter( "id", -1 );
        RegionObject regionObject = new RegionObject();
        if( id > 0 ){
            regionObject = regionObjectService.getById(id);
            if( regionObject == null ){
                model.put( "error.param.wrong", "无效参数" );
            }
        }
        model.addAttribute( "regionObject", regionObject );
//        model.addAttribute( "statusMap", FormSelector.getStatusMap() );
        setRequestModelMap(request, model);
        return view;
    }
	
	@RequestMapping("/save")
    public String saveRegionObject(HttpServletRequest request, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer id = reqInfo.getIntParameter( "id", -1 );
        if( id>0 ){                                         //更新
            RegionObject regionObject = regionObjectService.getById( id );
            if( bind( request, regionObject, model,  regionObjectValidator).hasErrors() ){
                return formView;
            }
            regionObjectService.update( regionObject );
        }else{                                              //添加
            RegionObject regionObject = new RegionObject();
            if( bind( request, regionObject, model,  regionObjectValidator).hasErrors() ){
                return formView;
            }
            regionObjectService.insert( regionObject );
        }
        model.clear();
        return redirectListView + "?" + reqInfo.getParameter( "queryParam", "" );
    }
	
	@RequestMapping("/updateStatus")
    public String updateRegionObjectStatus(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        Integer status = reqInfo.getIntParameter( "status", -1 );
        if( status < 0 ){
            model.clear();
            return redirectListView+ "?" + reqInfo.getParameter( "queryParam", "" );
        }
        setRequestModelMap(request, model);
        String objectIds = getGetObjectIds( reqInfo, model );
        if( objectIds.length() > 0 ){
            regionObjectService.update( model );
        }
        model.clear();
        return redirectListView + "?" + reqInfo.getParameter( "queryParam", "" );
    }

    @RequestMapping("/delete")
    public String deleteRegionObject(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        HttpRequestInfo reqInfo = new HttpRequestInfo(request);
        setRequestModelMap(request, model);
        String objectIds = getGetObjectIds( reqInfo, model );
        if( objectIds.length() > 0 ){
            regionObjectService.delete( model );
        }
        model.clear();
        return redirectListView + "?" + reqInfo.getParameter( "queryParam", "" );
    }
	
	private PageList<RegionObject> getPageList(ModelMap model) {
        if(model.containsKey("likeName"))
            model.put("likeName",model.get("likeName").toString().toLowerCase());
        PageList<RegionObject> regionObjectList = regionObjectService.getList( model, Integer.parseInt(model.get("pageNo").toString()), Integer.parseInt(model.get("pageSize").toString()) );
        model.addAttribute( "regionObjectList", regionObjectList);
        model.addAttribute( "objList", regionObjectList);
        model.addAttribute( "pageTurn", regionObjectList.getPageTurn());
        model.addAttribute( "titleMap", getPropertyTitleMap() );
        return regionObjectList;
    }

    private Map<String, String> getPropertyTitleMap() {
        if( propertyTitleMap == null ){
			propertyTitleMap = new HashMap<String,String>();
			propertyTitleMap.put( "id", "id" );
			propertyTitleMap.put( "regionId", "regionId" );
			propertyTitleMap.put( "objectId", "objectId" );
			propertyTitleMap.put( "objectUrl", "objectUrl" );
			propertyTitleMap.put( "objectType", "objectType" );
			propertyTitleMap.put( "topTime", "topTime" );
			propertyTitleMap.put( "createTime", "createTime" );
			propertyTitleMap.put( "createUserId", "createUserId" );
			propertyTitleMap.put( "modifyTime", "modifyTime" );
			propertyTitleMap.put( "modifyUserId", "modifyUserId" );
        }
        return propertyTitleMap;
    }
}
